using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Reflector;
using Reflector.CodeModel;
using Reflector.Framework;
using System.Reflection;
using Doubler.Helpers.Graph;

namespace ReflectorDouble
{
    public partial class CallGraphView : UserControl, IServiceComponent
    {       
#region Constructors

        public CallGraphView()
        {
            this._Services = null;
            this.Dock = DockStyle.Fill;
            InitializeComponent();
        }

#endregion

#region IServiceComponent Members

        private ReflectorServices _Services;

        public ReflectorServices Services
        {
            get
            {
                return _Services;
            }
            set
            {
                if (_Services != null)
                {
                    this.Services.AssemblyBrowser.ActiveItemChanged -= new EventHandler(this.activeItem_Changed);
                }
                _Services = value;
                if (_Services != null)
                {
                    this.Services.AssemblyBrowser.ActiveItemChanged += new EventHandler(this.activeItem_Changed);
                }
            }
        }

#endregion

#region Item Selection Change

        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);
            if (base.Parent != null)
            {
                this.PopulateView();
            }
        }

        public void activeItem_Changed(object sender, EventArgs args)
        {
            if (base.Parent != null)
            {
                this.PopulateView();
            }
        }
 
#endregion

        public void PopulateView()
        {
            if (this.Services.ActiveItem is ITypeReference)
            {
                
            }
            else if (this.Services.ActiveItem is IMethodDeclaration)
            {
                IMethodDeclaration DisassembledMethod = this.Services.Disassembler.TranslateMethodDeclaration(this.Services.ActiveMethod);
                CallGraph CallGraph;
                CallGraphBuilder Builder = new CallGraphBuilder(DisassembledMethod);
            	CallGraph = Builder.ConstructGraph();
                this.propertyGrid1.SelectedObject = CallGraph;
            }
            else if (this.Services.ActiveItem is IPropertyDeclaration)
            {
            	
            }
        }

    }
}
